package com.yy.job.service.impl;

import java.util.Date;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.google.common.base.Preconditions;
import com.google.common.collect.Lists;
import com.yy.job.common.dao.YYJobDOExtMapper;
import com.yy.job.common.dao.po.YYJobDO;
import com.yy.job.common.utils.date.YYDateUtils;
import com.yy.job.core.domain.JobDomain;

@Service
public class JobDomainService {
	
	private Logger log = LoggerFactory.getLogger(JobDomainService.class);
	
	@Autowired
	private YYJobDOExtMapper yyJobDOExtMapper;
	
	/**
	 * 从yy_job表中查询状态为("C")的数据
	 * 
	 * @param moduleCode
	 * @param start
	 * @param limit
	 * @return
	 */
	public List<JobDomain> listNewJobByPage(String projectCode, String moduleCode, Integer limit) {
		log.info("JobServiceImpl.listNewJobByPage projectCode={}, moduleCode={}, limit={}", projectCode, moduleCode, limit);
		Preconditions.checkArgument(StringUtils.isNotBlank(projectCode),"projectCode is empty");
		Preconditions.checkArgument(StringUtils.isNotBlank(moduleCode),"moduleCode is empty");
		Preconditions.checkArgument(limit != null,"limit is null");
		
		// 从yy_job表中查询状态为 CREATE("C", "创建")的数据
		List<YYJobDO> jobDOList = this.yyJobDOExtMapper.selectPendingNewJobByPage(projectCode,moduleCode, new Date(), limit);
		if (CollectionUtils.isEmpty(jobDOList)) {
			return Lists.newArrayList();
		}
		List<JobDomain> jobList = Lists.newArrayList();
		for (YYJobDO jobDO : jobDOList) {
			jobList.add(new JobDomain(jobDO));
		}
		return jobList;
	}
	
	/**
	 * 从yy_job表中查询状态为('L','F')的数据
	 * 
	 * @param moduleCode
	 * @param lastJobId
	 * @param limit
	 * @return
	 */
	public List<JobDomain> listPendingFailJobByPage(String projectCode, String moduleCode, Long lastJobId, Date pendingBackDate, Integer limit) {
		log.info("JobServiceImpl.listPendingFailJobByPage projectCode={}, moduleCode={}, lastJobId={}, pendingBackDate={}, limit={}", 
			projectCode, moduleCode, lastJobId, YYDateUtils.formatDateTimeDefault(pendingBackDate), limit);
		Preconditions.checkArgument(StringUtils.isNotBlank(projectCode),"projectCode is empty");
		Preconditions.checkArgument(StringUtils.isNotBlank(moduleCode),"moduleCode is empty");
		Preconditions.checkArgument(lastJobId != null,"lastJobId is null");
		Preconditions.checkArgument(pendingBackDate != null,"pendingBackDate is null");
		Preconditions.checkArgument(limit != null,"limit is null");
		
		List<YYJobDO> jobDOList = this.yyJobDOExtMapper.selectPendingFailJobByPage(projectCode,moduleCode, lastJobId, pendingBackDate, limit);
		if (CollectionUtils.isEmpty(jobDOList)) {
			return Lists.newArrayList();
		}
		List<JobDomain> jobList = Lists.newArrayList();
		for (YYJobDO jobDO : jobDOList) {
			jobList.add(new JobDomain(jobDO));
		}
		return jobList;
	}
	
}
